package com.jsb.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.jsb.mapper.PurchaseOrderMapper;
import com.jsb.model.Invoice;
import com.jsb.model.InvoiceExample;
import com.jsb.model.PurchaseOrder;
import com.jsb.model.PurchaseOrderExample;
import com.jsb.model.PurchaseOrderExample.Criteria;
import com.jsb.service.InvoiceService;

@Controller
public class InvoiceController{
	
	private int defaultPageSize=10;
	
	private String redirect = "redirect:/invoice/list.do";
	
	@Resource
	private InvoiceService invoiceService;
	
	@Resource
	private PurchaseOrderMapper purchaseOrderMapper;
	
	@RequestMapping("/invoice/list")
	public String list(
			Model model,
			@RequestParam(value = "pageNum", required = false) Integer pageNum,
			@RequestParam(value = "pageSize", required = false) Integer pageSize) {
		int maxPageNum = 0;
		if (pageSize == null) {
			pageSize = defaultPageSize;
		}
		InvoiceExample invoiceExample = new InvoiceExample();
		int totalCount = invoiceService.countInvoice(invoiceExample);
		if(totalCount%pageSize == 0){
			maxPageNum = totalCount/pageSize;
		}else{
			maxPageNum = totalCount/pageSize+1;
		}
		if (pageNum == null || pageNum < 1) {
			pageNum = 1;
		}else {
			if(maxPageNum > 0 && pageNum > maxPageNum) {
				pageNum = maxPageNum;
			}
		}
		invoiceExample.setPageIndex((pageNum - 1) * pageSize);
		invoiceExample.setPageSize(pageSize);
		List<Invoice> invoices = invoiceService.getPageInvoice(invoiceExample);
		//循环遍历invoices
		for (Invoice invoice : invoices) {
			String phoneNum = invoice.getUserPhoneNum();
			PurchaseOrderExample purchaseOrderExample = new PurchaseOrderExample();
			Criteria criteria = purchaseOrderExample.createCriteria();
			criteria.andPhoneNumEqualTo(phoneNum);
			List<PurchaseOrder> purchaseOrders = purchaseOrderMapper.selectByExample(purchaseOrderExample);
			for (PurchaseOrder purchaseOrder : purchaseOrders) {
//				invoice.addOrder(purchaseOrder);
			}
		}
		model.addAttribute("totalCount", totalCount);
		model.addAttribute("pageNum", pageNum);
		model.addAttribute("pageSize", pageSize);
		if(totalCount%pageSize == 0){
			model.addAttribute("maxPageNum", totalCount/pageSize);
		}else{
			model.addAttribute("maxPageNum", totalCount/pageSize+1);
		}
		model.addAttribute("invoices", invoices);
		return "/Invoice/list";
	}
	
	@RequestMapping("/invoice/toAdd")
	public String toAdd(Model model) {
		return "/Invoice/add";
	}

	@RequestMapping("/invoice/add")
	public String add(Model model, @ModelAttribute("invoice") Invoice invoice) {
		invoiceService.addInvoice(invoice);
		return redirect;
	}

	@RequestMapping("/invoice/del")
	public String del(Model model,
			@RequestParam(value = "uuid", required = true) String uuid) {
		invoiceService.delInvoiceByUuid(uuid);
		return redirect;
	}

	@RequestMapping("/invoice/toEdit")
	public String toEdit(Model model,
			@RequestParam(value = "uuid", required = true) String uuid) {
		Invoice invoice = invoiceService.getInvoiceByUuid(uuid);
		model.addAttribute("invoice", invoice);
		return "/Invoice/edit";
	}

	@RequestMapping("/invoice/edit")
	public String edit(Model model, @ModelAttribute("invoice") Invoice invoice) {
		invoiceService.updateInvoice(invoice);
		return redirect;
	}

	@RequestMapping("/invoice/view")
	public String view(Model model,
			@RequestParam(value = "uuid", required = true) String uuid) {
		Invoice invoice = invoiceService.getInvoiceByUuid(uuid);
		model.addAttribute("invoice", invoice);
		return "/Invoice/view";
	}
}
